package com.xinnet.mms.web;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xinnet.mms.entity.User;
import com.xinnet.mms.user.service.UserService;

@Controller
@RequestMapping(value = "/register")
public class RegisterController {
	@Autowired
	private UserService userService;
	/**
	 * 
	 * 功能描述：进入注册页
	 *
	 * @return
	 *
	 * @author 胡佳[hujia@xinnet.com]
	 *
	 * @since 2013-12-5
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping( method = RequestMethod.GET)
	public String register(){
		return "account/register";
	}
	
	/**
	 * 
	 * 功能描述：注册用户
	 *
	 * @param user
	 * @param br
	 * @return
	 *
	 * @author 胡佳[hujia@xinnet.com]
	 *
	 * @since 2013-12-5
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping( method = RequestMethod.POST)
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public String register(@Valid @ModelAttribute("user") User user, BindingResult br,RedirectAttributes redirectAttributes){
		if(br.hasErrors())
			return "account/register";
		else{
			userService.add(user);
			redirectAttributes.addFlashAttribute("loginName", user.getLoginName());
			return "redirect:/user";
		}
	}
}
